package com.musicdaze.graphics;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicBorders;

import com.musicdaze.utils.Utils;

/**
 * The UI for the application.
 * @author mfujihara
 *
 */
public class MusicDazeUI {
	/** the window frame */
	private static CustomFrame frame;
	/** the splash screen */
	private static Splash splash;
	/**
	 * static constructor
	 */
	static{
		create();
	}
	public static void main(String[] args){
		frame.setVisible(true);
	}
	/**
	 * creates the UI
	 */
	private static void create(){
		if (frame != null){
			return;
		}
		frame = new CustomFrame("MusicDaze");
		frame.setSize(400,400);
		frame.setLocationRelativeTo(null);
		splash = new Splash("splash.png");
		setup();
	}
	/**
	 * Setup for the MusicDaze
	 */
	private static void setup(){
		setupTray();
		JPanel schedule = new JPanel();
		schedule.setOpaque(false);
		schedule.setBorder(BorderFactory.createLineBorder(Color.blue,2));
		schedule.setLayout(new BoxLayout(schedule, BoxLayout.PAGE_AXIS));
		JLabel name = new JLabel("name");
		name.setForeground(Color.white);
		schedule.add(name);
		frame.getMain().add(schedule);
	}
	/**
	 * Gets the splash
	 * @return the splash
	 */
	public static Splash getSplash() {
		return splash;
	}
	/**
	 * Gets the frame
	 * @return the frame
	 */
	public static CustomFrame getFrame() {
		return frame;
	}
	/**
	 * Sets up the tray ui
	 */
	private static void setupTray() {
		try {
			UIManager.setLookAndFeel(
					UIManager.getSystemLookAndFeelClassName());
			// create system tray stuff
			SystemTray tray = SystemTray.getSystemTray();
			InputStream is =Utils.loadResource("trayicon.png");
			Image i = ImageIO.read(is);
			PopupMenu pop = new PopupMenu();

			// add view
			MenuItem config= new MenuItem("Config");
			config.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					frame.setVisible(true);
				}
			});
			pop.add(config);
			// add exit
			pop.addSeparator();
			MenuItem exit = new MenuItem("Exit");
			exit.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
			pop.add(exit);

			// create icon
			TrayIcon ti = new TrayIcon(i, "MusicDaze", pop);
			ti.setImageAutoSize(true);
			// add the try icon
			tray.add(ti);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}